package com.zjs.controller;

import com.github.pagehelper.PageInfo;
import com.gyf.crm.domain.Customer;
import com.gyf.crm.service.CustomerService;
import com.mysql.jdbc.StringUtils;
import com.zjs.common.bean.PageQuery;
import com.zjs.common.bean.ResponseResult;
import com.zjs.common.utils.PageUtils;
import com.zjs.common.utils.UUID32Gen;
import com.zjs.pojo.BcDecidedzone;
import com.zjs.pojo.BcStaff;
import com.zjs.pojo.BcSubarea;
import com.zjs.service.BcDecidedZoneI;
import com.zjs.service.BcSubAreaServiceI;
import com.zjs.service.Impl.BcStaffServiceImpl;
import com.zjs.vo.BcDecidedZoneVo;
import com.zjs.vo.BcSubareaVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名： BcDecidedZoneController
 * @说明： TODO
 * @作者：SS 苏帅出品
 * @时间：2019/12/10 15:27
 * @版本：1.0
 */
@Controller
@RequestMapping(value = "bcDecidedzone")
public class BcDecidedZoneController {


      @Autowired
      BcDecidedZoneI bcDecidedZoneI;

      @Autowired
      BcSubAreaServiceI bcSubAreaServiceI;

     @Autowired
     BcStaffServiceImpl bcStaffService;

      @Autowired

      private CustomerService customerService;


      @RequestMapping(value = "findPage.action")
      @ResponseBody
      public Map<String,Object> findPage(PageQuery pageQuery){

          PageInfo<BcDecidedzone> allBcDecidedZone = bcDecidedZoneI.findAllBcDecidedZone(pageQuery);

          Map<String,Object> map=new HashMap<>();

          List<BcDecidedZoneVo> bcDecidedZoneVos=new ArrayList<>();

          for (BcDecidedzone decidedzone:allBcDecidedZone.getList()){


              BcDecidedZoneVo bcDecidedZoneVo=new BcDecidedZoneVo();

              BeanUtils.copyProperties(decidedzone,bcDecidedZoneVo);


              BcStaff bcStaffById = bcStaffService.findBcStaffById(decidedzone.getStaffId());

              bcDecidedZoneVo.setStaff(bcStaffById);


              bcDecidedZoneVos.add(bcDecidedZoneVo);

          }


          map.put("total",allBcDecidedZone.getTotal());
          map.put("rows",bcDecidedZoneVos);

          return map;

      }




      //验证是否存在name
      @RequestMapping(value = "isExistsName.action")
      @ResponseBody
      public Boolean isExistsName(String name){


          Boolean existsName = bcDecidedZoneI.isExistsName(name);


          return existsName;

      }



      @RequestMapping(value = "saveWithBcSubarea.action")
      @ResponseBody
      public ResponseResult saveOrUpWithBcSubarea(String id, String name,String staffId,String ids){

          //封装到一个 对象中
          BcDecidedzone decidedzone=new BcDecidedzone();

          //使用  ,分割ids字符串
          String[] split = ids.split(",");

          int i=0;
          int j=0;

          //通过 id 判断是否为null 存在则是更新 否则是添加

          if (!StringUtils.isNullOrEmpty(id)){


              decidedzone.setId(id);
              decidedzone.setName(name);
              decidedzone.setStaffId(staffId);



              //更新操作
              i=bcDecidedZoneI.upDecidedZoneById(decidedzone);
              //清除之前 关于 传过来的 decided_id  为 null
              bcSubAreaServiceI.upDecidedIdStatus(id);

              for (String sid:split){



                  BcSubarea bcSubArea = bcSubAreaServiceI.findBcSubArea(sid);

                  bcSubArea.setDecidedzoneId(id);

                  //修改
                  int jCount= bcSubAreaServiceI.upBcSubAreaByObject(bcSubArea);


                  j+=jCount;
              }


          }else{

              String idU = UUID32Gen.gen32UUIDString();
              decidedzone.setId(idU);
              decidedzone.setName(name);
              decidedzone.setStaffId(staffId);

              i= bcDecidedZoneI.saveDecidedZone(decidedzone);

              for (String sid:split){

                  BcSubarea bcSubArea = bcSubAreaServiceI.findBcSubArea(sid);
                  bcSubArea.setDecidedzoneId(idU);

                  //修改
                  int jCount= bcSubAreaServiceI.upBcSubAreaByObject(bcSubArea);


                  j+=jCount;
              }

          }


          if (i>0 && j>0){

              return new ResponseResult(true,null);

          }


          return new ResponseResult(false,null);

      }


      //查询没有安排的客户
      @RequestMapping(value = "findNoSelCustomer.action")
      @ResponseBody
      public List<Customer> findNoSelCustomer(){


          List<Customer> customers = customerService.findnoassociationCustomers();


          return customers;
      }


      //查询当前定区下已经安排的定区
    @RequestMapping(value = "findSelectCustomer.action")
    @ResponseBody
    public List<Customer> findSelectCustomer(String id){


        List<Customer> customers = customerService.findhasassociationCustomers(id);


        return customers;

    }

    //执行关联客户
    @RequestMapping(value = "upCustomer.action")
    @ResponseBody
    public ResponseResult upCustomer(Integer[] customerIds,String id){


          customerService.assignCustomersToDecidedZone(customerIds,id);


          return new ResponseResult(true,null);
    }

    @RequestMapping(value = "delDecidedByIds.action")
    @ResponseBody
    public ResponseResult delDecidedByIds(String ids){

        String[] split = ids.split(",");


        for (String sid:split){

            int i = bcDecidedZoneI.delDecidedZoneById(sid);


            //更改BcSubArea的 decidedZone_id

            int j = bcSubAreaServiceI.upDecidedIdStatus(sid);

        }


        return new ResponseResult(true,null);
    }

    @RequestMapping(value = "upDecideZoneById.action")
    @ResponseBody
    public BcDecidedZoneVo  upDecideZoneById(String id){


        BcDecidedZoneVo decidedById = bcDecidedZoneI.findDecidedById(id);

        //通过id 得到List BcSubarea集合
        List<BcSubarea> subAreaByDecidedId = bcSubAreaServiceI.findSubAreaByDecidedId(id);

        decidedById.setBcSubareas(subAreaByDecidedId);


        return  decidedById ;
    }

}
